﻿using System.Threading.Tasks;
using Thinker.SignChannel.Entities;
using Thinker.SignChannel.Settings;
using Volo.Abp.Domain.Entities.Events;
using Volo.Abp.EventBus;
using Volo.Abp.Settings;
using Volo.Abp.Uow;

namespace Thinker.SignChannel.Results;

public abstract class ResultHandler(ISettingProvider settingProvider) :
    ILocalEventHandler<EntityCreatedEventData<Record>>
{
    protected readonly ISettingProvider settingProvider = settingProvider;

    public Task HandleEventAsync(EntityCreatedEventData<Record> eventData)
    {
        var record = eventData.Entity;
        return HandleCreateRecordAsync(record);
    }

    [UnitOfWork]
    // ReSharper disable once MemberCanBeProtected.Global
    // ReSharper disable once VirtualMemberNeverOverridden.Global
    public virtual async Task HandleCreateRecordAsync(Record record)
    {
        if (record.PersonId == null)
        {
            return;
        }

        if (record.TimeId == null)
        {
            return;
        }

        var calcResult =
            await settingProvider.GetAsync<int>(SignChannelSettings.ResultCreateStyle);
        await CreateAsync(record, calcResult);
    }

    protected abstract Task CreateAsync(Record record, int calcResult);
}